package com.zhuhanpeng.cloud.api.control;

import com.alibaba.fastjson.JSONObject;
import com.zhuhanpeng.cloud.spring.control.BaseController;
import org.apache.ibatis.annotations.Param;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author bjc:
 * @create 2023-02-21 下午 2:28
 */
@Controller
@RequestMapping("/api")
public class checkCodeController extends BaseController {
    private Logger logger = LogManager.getLogger(this.getClass());

    @RequestMapping("/checkCode")
    @ResponseBody
    public String checkCode(@Param("phone") String phone, @Param("phoneCode") String phoneCode, HttpServletRequest request) {
        try {
            //从session中取出验证码属性并转化为json数据结构
            JSONObject json = (JSONObject) request.getSession().getAttribute("phoneCode");
            logger.info("json:{}", json);
            //判断数据是否过期
//            if (json == null || System.currentTimeMillis() - json.getLongValue("createTime") > 1000 * 60 * 5) {
//                return failCodeJson("code.old", 110);
//            }
            //判断手机号是否正确
            if (!json.getString("phone").equals(phone)) {
                return failCodeJson("code.phone.fail", 111);
            }
            //判断验证码是否正确
            if (!json.getString("phoneCode").equals(phoneCode)) {
                return failCodeJson("code.false", 112);
            }
            return sucessJson("code.true");
        } catch (Exception e) {
            e.printStackTrace();
            return failCodeJson("code.false", 113);//113未知错误
        }
    }
}